package com.tarim.wheat.service.impl;

import com.tarim.wheat.mapper.UserMapper;
import com.tarim.wheat.model.User;
import com.tarim.wheat.service.UserService;
import com.tarim.wheat.vo.Criteria;
import com.tarim.wheat.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public int insert(User record) {
        Long userListSize =  userMapper.selectByUserName(record);
        if(userListSize > 0){
            return 0;
        }
        return userMapper.insert(record);
    }

    @Override
    public User selectForLogin(User record) {
        return userMapper.selectForLogin(record);
    }

    @Override
    public Long selectByUserName(User record) {
        return userMapper.selectByUserName(record);
    }

    @Override
    public PageBean selectByCondition(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", (pageBean.getPageIndex()-1) * pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        Long count = userMapper.selectByConditionCount(criteria);
        List<User> list = userMapper.selectByCondition(criteria);
        pageBean.setResultList(list);
        pageBean.setTotalCount(count);
        return pageBean;
    }

    @Override
    public int updateDr(User user) {
        return userMapper.updateDr(user);
    }

}
